import { ref } from 'vue'
import app from '@/app'
import store from '@/app/store'
import { get, update, del } from '@/api/erp/client_classify'

// 初始化
const classifyStore = store()

// 焦点
classifyStore.index = ref('all')

classifyStore.get = () => {
  return new Promise((resolve, jeject) => {
    get().then(res => {
      classifyStore.setList(res)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

classifyStore.update = (...arge) => {
  return new Promise((resolve, jeject) => {
    update(...arge).then(res => {
      classifyStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

classifyStore.del = (...arge) => {
  return new Promise((resolve, jeject) => {
    del(...arge).then(res => {
      classifyStore.index.value = 'all'
      classifyStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

classifyStore.getIndex = () => {
  return classifyStore.index.value == 'all' ? null : classifyStore.index.value
}

classifyStore.getIndexData = () => {
  return classifyStore.getList().find(item => item.id == classifyStore.getIndex())
}


export default classifyStore